package com.example.analyzevisio.service;

import com.example.analyzevisio.model.Connect;
import com.example.analyzevisio.model.connect.*;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.analyzevisio.utis.Obtain.*;

public class ConnectService {
    private static HashMap<String, Map<String, String>> dataMap;

    public void setDataMap(HashMap<String, Map<String, String>> dataMap){
        this.dataMap = dataMap;
    }

    /*
    连接线获取信息
     */
    public static Connect getConnect(Element shapeElement, NodeList shapeConnect, String text) {
        Connect connect = new Connect();
        connect.setShape("edge");

        Line line = new Line();
        line.setStroke("#808080");
        line.setStrokeWidth("0.5");
        line.setTargetMarker(new TargetMarker("block",
                "6","4","-4"));
        connect.setAttrs(new Attrs(line));

        String uniqueID = shapeElement.getAttribute("UniqueID");
        String substring = uniqueID.substring(1, uniqueID.length() - 1);

        connect.setId(substring);

        ConnectData connectData = new ConnectData();
        connectData.setStateType("edge");
        connectData.setLabel(text);
        connect.setData(connectData);

        connect.setZIndex("-1");

        ArrayList<Labels> labelsArrayList = new ArrayList<>();
        Labels labels = new Labels();
        ArrayList<Markup> markupArrayList = new ArrayList<>();
        Markup rectMarkup = new Markup();
        rectMarkup.setTagName("rect");
        rectMarkup.setSelector("labelBody");
        markupArrayList.add(rectMarkup);

        Markup textMarkup = new Markup();
        textMarkup.setTagName("text");
        textMarkup.setSelector("labelText");
        markupArrayList.add(textMarkup);

        labels.setMarkup(markupArrayList);

        LabelsAttrs labelsAttrs = new LabelsAttrs();
        HashMap<String, String> labelText = new HashMap<>();
        labelText.put("text",text);
        labelText.put("fill","#ffa940");
        labelText.put("textAnchor","middle");
        labelText.put("textVerticalAnchor","middle");
        labelText.put("fontSize","8");

        HashMap<String, String> labelBody = new HashMap<>();
        labelBody.put("ref","labelText");
        labelBody.put("refX","0");
        labelBody.put("refY","1");
        labelBody.put("refWidth","100%");
        labelBody.put("refHeight","100%");
        labelBody.put("refWidth2","3");
        labelBody.put("refHeight2","1");

        labelsAttrs.setLabelText(labelText);
        labelsAttrs.setLabelBody(labelBody);

        labels.setAttrs(labelsAttrs);

        Position position = new Position();
        position.setDistance("0.3");
        Args args = new Args(true, true);
        position.setArgs(args);

        labels.setPosition(position);

        labelsArrayList.add(labels);
        connect.setLabels(labelsArrayList);
        //连接点信息
        String id = shapeElement.getAttribute("ID");
        double BeginX = Math.ceil( Double.parseDouble(getCellValue(shapeElement, "BeginX"))*25.4);
        double BeginY = Math.ceil( Double.parseDouble(getCellValue(shapeElement, "BeginY"))*25.4);

        double EndX = Math.ceil( Double.parseDouble(getCellValue(shapeElement, "EndX"))*25.4);
        double EndY = Math.ceil( Double.parseDouble(getCellValue(shapeElement, "EndY"))*25.4);

        //判断连接线是否连接到节点
        boolean existEndX = false,existBeginX = false;
        for (int i = 0; i < shapeConnect.getLength(); i++) {
            Element connectElement = (Element) shapeConnect.item(i);
            String fromSheetId = connectElement.getAttribute("FromSheet");
            if (id.equals(fromSheetId)){
                String fromCell = connectElement.getAttribute("FromCell");

                String toSheetId = connectElement.getAttribute("ToSheet");
//                String toPart = connectElement.getAttribute("ToPart");

                Map<String, String> stringMap = dataMap.get(toSheetId);
                double pinX = Double.parseDouble(stringMap.get("pinX"));
                double PinY = Double.parseDouble(stringMap.get("PinY"));




                if ("EndX".equals(fromCell)){
                    existEndX = true;
                    // 判断节点是否正常
                    ConnectionPointJudgment(toSheetId,false);

                    String extracted = getToPart(pinX,PinY,EndX,EndY);
                    connect.setTarget( new Start(stringMap.get("UniqueID"), extracted));
                }else if ("BeginX".equals(fromCell)){
                    existBeginX = true;
                    // 判断节点是否正常
                    ConnectionPointJudgment(toSheetId,true);

                    String extracted = getToPart(pinX,PinY,BeginX,BeginY);
                    connect.setSource(new Start(stringMap.get("UniqueID"), extracted));
                }
            }
        }
        if (!(existEndX && existBeginX)){
            throw new RuntimeException("\"" + text +"\"" + "存在连接线未连接节点，请注意查看");
        }
        List<String> matches = getStrings(shapeElement);
        for (String match : matches) {
            if (dataMap.containsKey(match)){
                Map<String, String> stringMap = dataMap.get(match);
                connect.setParent(stringMap.get("UniqueID"));
            }
        }
        return connect;
    }
}
